<?php //Accessing Substrings
//The return value from  strpos() is the first position in the string (the “haystack”) at which the substring (the “needle”) was found. 
// strpos(haystack, needle)
// If the needle wasn’t found at all in the haystack, strpos()returns false.
//To differentiate between return values of 0 and false, you must use the identity operator (===) or the not–identity operator (!==) instead of regular equals (==) or not-equals (!=). Example below compares the return value from strpos() to false using ===
$_POST["email"] = 'Anhph68@gmail.com';
if (strpos($_POST["email"], '@') === false) {
	print 'There is no @ in the email address';
}else{
	print substr($_POST["email"], 0, strpos($_POST["email"], '@')).'<br>';	//Extracting Substrings from 0 to strpos($_POST["email"], '@')
	print substr($_POST["email"], strpos($_POST["email"], '@')+1); //Extracting Substrings from strpos($_POST["email"], '@')+1
}
$str = 'watch out for that tree';
// substr(string, start)
//If $start is bigger than the length of the string, substr()returns false..

print '<br>'.strlen($str).'<br>'.substr($str,6,5);
print '<br>'.substr($str,20,5);

//If $startis negative, substr()counts back from the end of the string to determine where your substring starts
print '<br>'.substr($str,-10);
print '<br>'.substr($str,-17,15);
//With a negative $start value that goes past the beginning of the string (for example, if $startis −27 with a 20-character string), substr() behaves as if $startis 0.
// If $lengthis negative, substr()counts back from the end of the string to determine where your substring ends
print '<br>'.substr('watch out for that tree',15,-2);
print '<br>'.substr('watch out for that tree',-4,-1);
?>

<?php 
// Everything from position $start to the end of $old_string becomes $new_substring
// $new_string = substr_replace($old_string,$new_substring,$start);
// $length characters, starting at position $start, become $new_substring
// $new_string = substr_replace($old_string,$new_substring,$start,$length);
$myStr = 'My pet is a blue dog.';
print '<br>'.substr_replace($myStr, 'fish.', 12);
print '<br>'.substr_replace($myStr, 'green', 12, 4);
$credit_cards = '4111 1111 1111 1111';
print '<br>'.substr_replace($credit_cards, 'xxxx ', 0, strlen($credit_cards)-4);
//If  $startis negative, the new substring is placed at  $startcharacters counting from the end of $old_string, not from the beginning
print '<br>'.substr_replace($myStr, 'fish.', -9);
print '<br>'.substr_replace($myStr, 'green', -9, 4);
//If $startand $lengthare 0, the new substring is inserted at the start of $old_string
print '<br>'.substr_replace($myStr, 'Title: ', 0, 0);
?>

<?php //The function substr_replace(  )is useful when you’ve got text that’s too big to display all at once, and you want to display some of the text with a link to the rest.
//The statements below could have been useful when you have learned how to connect with database (MySQL, SQL Server)
// $r = mysql_query("SELECT id,message FROM messages WHERE id = $id") or die();
// $ob = mysql_fetch_object($r);
// printf('<a href="more-text.php?id=%d">%s</a>',
// $ob->id, substr_replace($ob->message,' ...',25));
?>